package com.chenchao.thread;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/17 11:20
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test8.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */

class 运货 implements Runnable {
    Thread 运货司机, 装运工, 仓库管理员;
    final int 货物数量 = 10;
    final int 仓库门数 = 5;

    public 运货() {
        运货司机 = new Thread(this, "运货司机");
        装运工 = new Thread(this, "装运工");
        仓库管理员 = new Thread(this, "仓库管理员");
    }

    @Override
    public void run() {
        if ("运货司机".equals(Thread.currentThread().getName())) {
            System.out.println(Thread.currentThread().getName() + "等待装运工装货");
            try {
                装运工.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "打开车锁");
            System.out.println(Thread.currentThread().getName() + "把握方向盘");
            System.out.println(Thread.currentThread().getName() + "挂档");
            System.out.println(Thread.currentThread().getName() + "踩油门");

        }
        if ("装运工".equals(Thread.currentThread().getName())) {
            System.out.println(Thread.currentThread().getName() + "等待仓库管理员开门");
            try {
                仓库管理员.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 1; i <= 货物数量; i++) {
                System.out.println(Thread.currentThread().getName() + "搬运第" + i + "箱货物到货车");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if ("仓库管理员".equals(Thread.currentThread().getName())) {
            for (int i = 1; i <= 仓库门数; i++) {
                System.out.println(Thread.currentThread().getName() + "打开第" + i + "道门");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

public class Test8 {
    public static void main(String[] args) {
        运货 沃尔玛 = new 运货();
        沃尔玛.仓库管理员.start();
        沃尔玛.装运工.start();
        沃尔玛.运货司机.start();
    }
}
